package com.hsogoo.study.concurrency.disruptor;

import java.util.concurrent.Executors;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

/**
 * Created by za-huangsaigang on 2018/10/26.
 */
public class DisruptorTest {

    public static void main(String[] args) {

        int bufferSize = 1024*1024;//环形队列长度，必须是2的N次方
        TaskEventFactory eventFactory = new TaskEventFactory();
        /**
         * 定义Disruptor，基于单生产者，阻塞策略
         */
        Disruptor<TaskEvent> disruptor = new Disruptor<TaskEvent>(eventFactory,bufferSize, Executors.newCachedThreadPool(), ProducerType.SINGLE,new BlockingWaitStrategy());
        disruptor.handleEventsWithWorkerPool(new ConsumerHandler());
        disruptor.start();
        RingBuffer<TaskEvent> ringBuffer = disruptor.getRingBuffer();
        for(int i=0; i< 10000; i++){
            ringBuffer.publishEvent(new TaskEventTranslator(), new Task(i));
        }

//        disruptor.shutdown();
    }
}
